<?php

require_once 'OpenID/Extension.php';
require_once 'Validate.php';

class OpenID_Extension_AX extends OpenID_Extension
{
    protected $namespace ='http://openid.net/srv/ax/1.0';
    protected $alias = 'ax';

    protected $validModes = array(
        'fetch_request',
        'fetch_response',
        'store_request',
        'store_response_success',
        'store_response_failure',
    );

    /**
     * Adds some validation checking when setting a key, then calls the parent set()
     * 
     * @param string $key   Message key
     * @param mixed  $value Key's value
     * 
     * @return void
     */
    public function set($key, $value)
    {
        if (strpos($key, 'mode') === 0) {
            if (!in_array($value, $this->validModes)) {
                throw new OpenID_Extension_Exception('Invalid AX mode: ' . $key);
            }
        }

        if (preg_match('/^type[.]/', $key)) {
            if (!Validate::uri($value)) {
                throw new OpenID_Extension_Exception($key . ' is not a valid URI');
            }
        }
        parent::set($key, $value);
    }
}
?>
